package com.example.verticallayoutview;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/8/30.
 */

public class VerticalLayoutView extends ViewGroup {

    public VerticalLayoutView(Context context) {
        super(context);
    }

    public VerticalLayoutView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //onMeasure

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //如果是wrap_content,宽应该是多大？
        //宽：子控件中最大的那个宽度
        //高：所有子控件的高度之和
        //提前去测量一个子控件，不然拿不到子控件
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //widthMeasureSpec 由两个int值组成的
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int childWidthMax = getChildWidthMax();
        int childHeightSum = getChildHeightSum();
        //Mode   三种模式:UNSPECIFIED, AT_MOST or EXACTLY
        //UNSPECIFIED 代表父控件对你的尺寸不做任何限制
        //AT_MOST   代表父控件对你的尺寸设置了最大值,你不能超过这个上限
        //EXACTLY   代表这个控件的大小已经是一个确定的值      50dp    px  sp match_parent(前提：父控件的尺寸已经确定)
        switch (widthMode) {
            case MeasureSpec.UNSPECIFIED:
                Log.d(getClass().getSimpleName(), "onMeasure: " + "当前模式为:UNSPECIFIED");
                break;
            case MeasureSpec.AT_MOST:
                Log.d(getClass().getSimpleName(), "onMeasure: " + "当前模式为:AT_MOST");
                widthSize = childWidthMax;
                heightSize = childHeightSum;
                break;
            case MeasureSpec.EXACTLY:
                Log.d(getClass().getSimpleName(), "onMeasure: " + "当前模式为:EXACTLY");
                break;
            default:
                break;
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    //设置子控件的排列
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //将子控件布局到合适的位置
        //遍历子控件，调用子控件的layout方法，去布局到合适的位置
        int childCount = getChildCount();
        int top = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            //假如是第一个子控件
            childAt.layout(0,top,childAt.getMeasuredWidth(),top+childAt.getMeasuredHeight());
            top+=childAt.getMeasuredHeight();
        }
    }

    //获得子控件中最大的宽度
    public int getChildWidthMax() {
        int childCount = getChildCount();
        int maxWidth = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            //直接通过getWidth是拿不到宽度的
//            int width = childView.getWidth();
            //写自定义控件时,要通过getMeasuredWidth来拿宽度的
            //有个前提,需要先去测量一下这个子控件,你才能拿得到
            int width = childAt.getMeasuredWidth();
            Log.d(getClass().getSimpleName(), "getChildWidthMax: width：" + width);
            if (maxWidth < width) {
                maxWidth = width;
            }
        }
        return maxWidth;
    }

    //获得子控件中所有子控件高度之和
    public int getChildHeightSum() {
        int childCount = getChildCount();
        int sumHeight = 0;
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            int measuredHeight = childAt.getMeasuredHeight();
            sumHeight += measuredHeight;
        }
        Log.d(getClass().getSimpleName(), "getChildHeightSum: sumHeight：" + sumHeight);
        return sumHeight;
    }
}
